package com.telecomyt.jwportal.activity.login;

import android.app.Activity;

import com.telecomyt.jwportal.activity.login.bean.LoginBean;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.http.RetrofitManager;
import com.telecomyt.jwportal.http.base.RxHelper;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author created by chengDa
 * @time 2020/8/25  16:44
 * @description
 */
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(LifecycleProvider<ActivityEvent> provider) {
        super(provider);
    }

    @Override
    public void toLogin(String username, String password) {
        getView().showLoading();
        RetrofitManager.getInstance()
                .getDefaultReq()
                .login(username, password)
                .compose(RxHelper.observableIOMain((Activity) getProvider()))
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(LoginBean bean) {
                        if (bean != null) {
                            if (bean.getErrorCode() == 0) {
                                getView().loginSuccess(bean);
                            } else {
                                ToastUtils.showShort(bean.getErrorMsg());
                            }
                        }
                        getView().dismissLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().dismissLoading();

                    }

                    @Override
                    public void onComplete() {
                        getView().dismissLoading();
                    }
                });
    }

    @Override
    public void loginOut() {
        getView().showLoading();
        RetrofitManager.getInstance()
                .getDefaultReq()
                .logout()
                .compose(getProvider().<Object>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object object) {
                        getView().loginOutSuccess();
                        getView().dismissLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().dismissLoading();

                    }

                    @Override
                    public void onComplete() {
                        getView().dismissLoading();
                    }
                });
    }

    @Override
    protected LoginContract.Model creatModule() {
        return null;
    }
}
